package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.ChargingPileDao;
import cn.topevery.garden.dal.dao.FacilitiesDao;
import cn.topevery.garden.dal.util.GeometryExtHelper;
import cn.topevery.garden.entity.dto.ChargingPileDTO;
import cn.topevery.garden.entity.po.ChargingPile;
import cn.topevery.garden.entity.po.Facilities;
import cn.topevery.garden.entity.po.FacilitiesFile;
import cn.topevery.garden.entity.query.ChargingPileQuery;
import cn.topevery.garden.framework.base.BaseLogic;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.dto.PointDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Service
public class ChargingPileLogic extends BaseLogic<ChargingPile> {
    @Autowired
    ChargingPileDao chargingPileDao;

    @Autowired
    private FacilitiesDao facilitiesDao;

    @Autowired
    private FacilitiesFileLogic fileLogic;

    @Autowired
    private Mapper mapper;

    @Override
    public BaseEntityDao<ChargingPile, Integer> getDao() {
        return chargingPileDao;
    }

    

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<ChargingPile> listPage(ChargingPileQuery query) {
        return chargingPileDao.listPage(query);
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        chargingPileDao.delete(userId, ids);
    }

    public void saveOrUpdate(String userId, ChargingPileDTO dto) {
        Facilities facilities = new Facilities();
        facilities.setId(dto.getFacilitiesId());
        facilities.setCode(dto.getCode());
        facilities.setParkId(dto.getParkId());
        facilities.setParkName(dto.getParkName());
        facilities.setExtentId(dto.getExtentId());
        facilities.setExtentName(dto.getExtentName());
        facilities.setTypeId("d930fa33-8c38-4fd4-bc3f-c283d44fe687");
        facilities.setTypeName("充电桩");
        facilities.setAddress(dto.getAddress());
        facilities.setStatus((short) 1);
        facilities.setDesc(dto.getDesc());
        facilities.setGeomType(0);
        List<PointDto> pointDtoList = new ArrayList<>();
        pointDtoList.add(new PointDto(dto.getLngBd09(), dto.getLatBd09()));
        facilities.setGeom(GeometryHelper.getGeometry(GeometryExtHelper.PointToWkt(pointDtoList)));
        facilitiesDao.saveOrUpdate(userId, facilities);
        if (dto.getFacilitiesId() != null) {
            fileLogic.delByMainId(userId, dto.getFacilitiesId());
        }
        fileLogic.delByMainId(userId, facilities.getId());
        if (dto.getFileList() != null && !dto.getFileList().isEmpty()) {
            for (FacilitiesFile file : dto.getFileList()) {
                file.setMainId(facilities.getId());
                file.setDbLastUpdateDate(LocalDateTime.now());
                fileLogic.saveOrUpdate(userId, file);
            }
        }
        dto.setFacilitiesId(facilities.getId());
        chargingPileDao.saveOrUpdate(userId, mapper.map(dto, ChargingPile.class));
    }
}
